<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.sql.*" %>
<html>
<head>
    <title>Student Information</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f9;
            margin: 0;
            padding: 20px;
        }
        h1 {
            text-align: center;
            color: #333;
        }
        table {
            width: 80%;
            margin: 20px auto;
            border-collapse: collapse;
            background-color: #fff;
            box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
        }
        th, td {
            padding: 12px 15px;
            text-align: center;
            border: 1px solid #ddd;
        }
        th {
            background-color: #007bff;
            color: white;
        }
        tr:nth-child(even) {
            background-color: #f9f9f9;
        }
        tr:hover {
            background-color: #f1f1f1;
        }
    </style>
</head>
<body>
<h1>学生信息列表</h1>
<table>
    <tr>
        <th>ID</th>
        <th>学号</th>
        <th>姓名</th>
        <th>性别</th>
        <th>创建日期</th>
    </tr>
    <%
        Connection conn = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            // 加载数据库驱动
            Class.forName("com.mysql.cj.jdbc.Driver");

            // 数据库连接信息
            String url = "jdbc:mysql://127.0.0.1:3306/stu";
            String username = "root";
            String password = "123456";

            // 建立数据库连接
            conn = DriverManager.getConnection(url, username, password);

            // 执行查询
            stmt = conn.createStatement();
            String sql = "SELECT id, stuno, stuname, sex, createdate FROM student";
            rs = stmt.executeQuery(sql);

            // 遍历结果集并显示数据
            while (rs.next()) {
                int id = rs.getInt("id");
                String stuno = rs.getString("stuno");
                String stuname = rs.getString("stuname");
                String sex = rs.getString("sex");
                Date createDate = rs.getDate("createdate");
    %>
    <tr>
        <td><%= id %></td>
        <td><%= stuno %></td>
        <td><%= stuname %></td>
        <td><%= sex%></td>
        <td><%= createDate %></td>
    </tr>
    <%
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            // 关闭资源
            if (rs != null) rs.close();
            if (stmt != null) stmt.close();
            if (conn != null) conn.close();
        }
    %>
</table>
</body>
</html>